<template>
	<view class="content">
		<!-- 从主入口进入 -->
		<view class="uni-list" v-for="(fromList,index) in mainLogList" :key="index" v-if="isFromMain">
			<view class="uni-list-cell uni-collapse">
				<view class="uni-list-cell-navigate uni-navigate-bottom" hover-class="uni-list-cell-hover" :class="fromList.open ? 'uni-active' : ''"
				 @click="triggerCollapse(index,'firstList',fromList)">
					{{fromList.project_name}}
				</view>
				<view class="uni-list uni-collapse" :class="fromList.open ? 'uni-active' : ''">
					<view class="uni-list" v-for="(item,i) in fromList.projects_info" :key="i">
						<view class="uni-list-cell uni-collapse">
							<view class="uni-list-cell-navigate uni-navigate-bottom" hover-class="uni-list-cell-hover" @click="triggerCollapse(i,'secondList')">
								{{item.month}}月份日志({{item.total_total}}个,突发事件{{item.total_emergency}}个)
							</view>
							<view class="uni-list uni-collapse" :class="item.open ? 'uni-active' : ''">
								<cus-calendar-log :currentDay="item.month" :checks="item.info" v-on:click="goDetailDate" />
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 从项目进入 -->
		<view class="uni-list" v-for="(list,index) in logList" :key="index" v-if="!isFromMain">
			<view class="uni-list-cell uni-collapse">
				<view class="uni-list-cell-navigate uni-navigate-bottom" hover-class="uni-list-cell-hover" :class="list.open ? 'uni-active' : ''"
				 @click="triggerCollapse(index)">
					{{list.month}}月份日志({{list.total_total}}个,突发事件{{list.total_emergency}}个)
				</view>
				<view class="uni-list uni-collapse" :class="list.open ? 'uni-active' : ''">
					<cus-calendar-log :currentDay="list.month" :checks="list.info" v-on:click="goDetailDate" />
				</view>
			</view>
		</view>
		<!-- <button @click="goAddConstruction()">点击添加</button> -->
		<uni-load-more v-if="!noData" :loadingType="loadingType" :contentText="contentText"></uni-load-more>
		<view v-if="noData" class="cus-no-data">
			<text class="def-icon">&#xe64b;</text>
			<text> 暂 无 数 据 . . . </text>
		</view>
	</view>
</template>
<script>
	import cusCalendarLog from '@/components/cus-calendar-log.vue'
	export default {
		components: {
			cusCalendarLog
		},
		data() {
			return {
				logList: [], //施工列表数据(从项目进入)
				mainLogList: [], //施工列表数据(从主入口进入)
				clickFirstList: '', //点击哪个第一行(从主入口进入)
				loadingType: 0,
				noData: false, //增项列表数据是否为空
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了"
				},
				//列表搜索条件
				formData: {
					hard_id: '', //是项目id
					page_no: 1,
					page_size: 31
				},
				isFromMain: false //判断是否从主入口进入
			}
		},
		onLoad(param) {
			if (!param.id) { //没有id是从主入口进入
				this.isFromMain = true
			} else {
				this.formData.hard_id = param.id
			}
			this.getConstructionLog()
			this.$eventBus.$on("constructionLogEditExecute", ()=>{
				this.getConstructionLog()
			})
		},
		onReachBottom() {
			if (this.loadingType !== 0) {
				return;
			}
			this.searchForm.page_no++
			this.loadingType = 1
			const tempPath = this.isFromMain ? '/lwj/app_project_log_search_all' : '/lwj/app_project_log_search'
			uni.request({
				url: this.$store.state.baseUrl + tempPath,
				method: 'POST',
				header: {
					token: this.$store.state.token
				},
				data: this.searchForm,
				success: res => {
					if (res.data.code === 1) {
						const data = res.data.data.list
						if (this.isFromMain) {
							data.map((item, i) => {
								data[i].open = false
								data[i].projects_info.map((info, index) => {
									data[i].projects_info[index].open = false
								})
							})
							this.mainLogList.push(...data)
						} else {
							data.map((item, i) => {
								data[i].open = false
							})
							this.logList.push(...data)
						}
						if (data.length < this.searchForm.page_size) {
							this.loadingType = 2
						} else {
							this.loadingType = 0
						}
					}
				},
				fail: () => {},
				complete: () => {}
			});
		},
		// //标题栏中右上角新增按钮
		// onNavigationBarButtonTap() {
		// 	this.goAddConstruction()
		// },
		methods: {
			//获取施工日志列表
			getConstructionLog() {
				uni.showLoading({
					title: '',
					mask: false
				});
				const tempPath = this.isFromMain ? '/lwj/app_project_log_search_all' : '/lwj/app_project_log_search'
				this.$request({
					url: this.$store.state.baseUrl + tempPath,
					method: 'POST',
					header: {
						token: this.$store.state.token
					},
					data: {
						...this.formData
					},
					success: res => {
						if (res.data.code === 1) {
							if (res.data.data.list.length === 0) {
								this.noData = true
							} else {
								this.noData = false
								const data = res.data.data.list
								if (this.isFromMain) {
									data.map((item, i) => {
										data[i].open = false
										data[i].projects_info.map((info, index) => {
											data[i].projects_info[index].open = false
										})
									})
									this.mainLogList = data
								} else {
									data.map((item, i) => {
										data[i].open = false
									})
									this.logList = data
								}
							}
							if (res.data.data.list.length < this.formData.page_size) {
								this.loadingType = 2
							} else {
								this.loadingType = 0
							}
						}else if(res.data.code === 0){
							this.noData = true
							uni.showToast({
								title: res.data.msg,
								mask: false,
								duration: 1500,
								icon:'none'
							});
						}
					},
					fail: () => {},
					complete: () => {
					}
				});
			},
			//e点击的是第几行，which分第一行和第二行（从主入口进去）
			triggerCollapse(e, which = '', fromList) {
				if (this.isFromMain) {
					//是从主入口进入的，会多一层列表
					if (which === 'firstList') {
						this.formData.hard_id = fromList.project_id
						this.clickFirstList = e
						for (var i = 0; i < this.mainLogList.length; ++i) {
							if (e === i) {
								this.mainLogList[i].open = !this.mainLogList[e].open;
							} else {
								this.mainLogList[i].open = false;
							}
						}
					} else if (which === 'secondList') {
						for (var i = 0; i < this.mainLogList[this.clickFirstList].projects_info.length; ++i) {
							if (e === i) {
								this.mainLogList[this.clickFirstList].projects_info[i].open = !this.mainLogList[this.clickFirstList].projects_info[
									e].open;
							} else {
								this.mainLogList[this.clickFirstList].projects_info[i].open = false;
							}
						}
					}
				} else {
					//是从项目进入的
					for (var i = 0; i < this.logList.length; ++i) {
						if (e === i) {
							this.logList[i].open = !this.logList[e].open;
						} else {
							this.logList[i].open = false;
						}
					}
				}
			},
			//进入到详情页面	
			goDetailDate(param) {
				//如果有日志id，就进入编辑页面，如果没有就进入新增页面
				if (param.log_id) { //编辑页面
					const url = this.isFromMain ? '&isFromMain=' + this.isFromMain : ""
					uni.navigateTo({
						url: './constructionLogEdit?hard_id=' + this.formData.hard_id + '&log_id=' + param.log_id + url,
						success: res => {},
						fail: () => {},
						complete: () => {}
					});
				} else { //新增页面
					this.goAddConstruction(param.date)
				}
			},
			//进入到添加页面
			goAddConstruction(date) {
				const path = this.isFromMain ? '&isFromMain=' + this.isFromMain : ""
				uni.navigateTo({
					url: './constructionLogEdit?hard_id=' + this.formData.hard_id + '&date=' + date + path,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style scoped>
	.uni-list-cell-navigate {
		padding: 30upx;
	}
	.uni-list{
		display: block;
	}
</style>

